class Solution {
private:
    int m;
    int n;
    int binary_search(vector<vector<int>>& matrix, int i, int j, int target){
        if(i > j)return -1;
        int mid = (i + j) / 2;
        int temp = matrix[mid / n][mid % n];
        if(temp == target)return mid;
        else if(temp < target)return binary_search(matrix, i + 1, j, target);
        else return binary_search(matrix, i, j - 1, target);
    }
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        this->m = matrix.size();
        this->n = matrix[0].size();
        return binary_search(matrix, 0, m * n - 1, target) != -1;
    }
};

//2021.3.30 
